package mvpfoot.ui;

import android.Manifest;
import android.os.Bundle;
import android.support.design.widget.TextInputLayout;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.click.mvpfoot.R;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import mvpfoot.BaseActivity;
import mvpfoot.entity.User;
import mvpfoot.iViewBind.IRegister;
import mvpfoot.listener.PermissionListener;
import mvpfoot.presenter.LoginPresenter;


public class LoginActivity extends BaseActivity implements TextView.OnEditorActionListener, IRegister, View.OnClickListener {

    @InjectView(R.id.login_user_et)
    EditText loginUserEt;
    @InjectView(R.id.login_pd_et)
    EditText loginPdEt;
    @InjectView(R.id.login_user_input)
    TextInputLayout loginUserInput;
    @InjectView(R.id.login_pd_input)
    TextInputLayout loginPdInput;
    @InjectView(R.id.login_button)
    Button loginButton;

    private String TAG = "login";
    public LoginPresenter loginPresenter = new LoginPresenter(this);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.inject(this);
        initView();
        Log.d(TAG, "onClick() called with: " + "v = [" + User.getInstance().getName() + "]");

    }
    @Override
    public void initView() {
        loginButton.setOnClickListener(this);
        loginPdEt.setOnEditorActionListener(this);
        if (User.getInstance().getLoginStatus()){
            startActivity(PersonCenterActivity.class);
            finish();
        }
    }
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if (v.getId() == R.id.login_pd_et) {
            if (actionId == EditorInfo.IME_ACTION_DONE) {
                Log.d(TAG, "onEditorAction() called with: ");
                regist();
                return true;
            }

        }
        return false;
    }

    private void regist() {
        String user = loginUserEt.getText().toString();
        String pwd = loginPdEt.getText().toString();
        if (user.length() < 3) {
            loginUserInput.setError("用户名格式不对");
            loginUserInput.setErrorEnabled(true);
            return;
        } else {
            loginUserInput.setError("格式正确");
            loginUserInput.setErrorEnabled(false);
        }
        showDialog("正在登录");
        loginPresenter.login(user, pwd);

    }



    @Override
    public void register(String name, String pwd) {

       Toast.makeText(this,name+"欢迎回来",Toast.LENGTH_SHORT).show();
        hideDialog();
        startActivity(PersonCenterActivity.class);
    }

    public void loadSuccess() {

    }

    public void loaderror(String error) {

       Toast.makeText(this,error+"登录失败",Toast.LENGTH_SHORT).show();
        hideDialog();
    }


    @Override
    public void onClick(View v) {
        requestRuntimePermission(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, new PermissionListener() {
            @Override
            public void granted() {
                Toast.makeText(LoginActivity.this,"权限请求成功",Toast.LENGTH_SHORT).show();
                regist();
            }

            @Override
            public void denied(List<String> denidePermission) {
                for (int i = 0; i < denidePermission.size(); i++) {
                    Toast.makeText(LoginActivity.this,denidePermission.get(i)+"请求失败",Toast.LENGTH_SHORT).show();
                }
            }
        });

    }
}
